package com.moon;

import io.rong.imkit.fragment.ConversationFragment;
import io.rong.imlib.model.Conversation;

import java.util.Locale;

import cn.yueliangbaba.R;

import com.alibaba.fastjson.JSONObject;
import com.sys.DemoApp;

import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.Resources;

public class ChatActivity extends FragmentActivity implements OnClickListener{
	// MyAdapter mAdapter;
	// JSONArray mArrayList = new JSONArray();

	/**
	 * 目标 Id
	 */
	private String mTargetId;

	/**
	 * 刚刚创建完讨论组后获得讨论组的id 为targetIds，需要根据 为targetIds 获取 targetId
	 */

	/**
	 * 会话类型
	 */
	private Conversation.ConversationType mConversationType;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_chat);
		
		if(MainTabActivity.mMainTabActivity != null) MainTabActivity.mMainTabActivity.setChatMsg();
		
		findViewById(R.id.left).setOnClickListener(this);
		findViewById(R.id.right).setOnClickListener(this);
		getIntentDate(getIntent());
		
		if(mConversationType == Conversation.ConversationType.GROUP)  ((Button) findViewById(R.id.right))
		.setCompoundDrawablesWithIntrinsicBounds(
				R.drawable.btn_classcircle, 0, 0, 0);
		else findViewById(R.id.right).setVisibility(View.INVISIBLE);
		//((Button) findViewById(R.id.right)).setCompoundDrawablesWithIntrinsicBounds(R.drawable.icon_identity, 0, 0, 0);
	
//		InputProvider.ExtendProvider[] provider1 = {
//				new ImageInputProvider(RongContext.getInstance()),//图片
//				new NewCameraInputProviders(RongContext.getInstance()),//相机
//				new LocationInputProvider(RongContext.getInstance()),//地理位置
//				}; 
	}
	
	protected void onResume()
	{
		super.onResume();
		setTitle();
	}
	
	void setTitle()
	{
		if(mConversationType == Conversation.ConversationType.GROUP) 
		{
			String classname = ((DemoApp)getApplicationContext()).mClasses.getString(mTargetId);
			((TextView) findViewById(R.id.center)).setText(classname);
		}
		else
		{
			JSONObject member = ((DemoApp)getApplicationContext()).mMembers.getJSONObject(mTargetId);
			if(member == null) return;
			
			String name = member.getString("username");
			if(member.getString("alias") != null && member.getString("alias").length() != 0) name = member.getString("alias");
			((TextView) findViewById(R.id.center)).setText(name);
		}
	}

	/**
	 * 展示如何从 Intent 中得到 融云会话页面传递的 Uri
	 */
	private void getIntentDate(Intent intent) {
		mTargetId = intent.getData().getQueryParameter("targetId");
		mConversationType = Conversation.ConversationType.valueOf(intent
				.getData().getLastPathSegment()
				.toUpperCase(Locale.getDefault()));

		enterFragment(mConversationType, mTargetId);
	}

	/**
	 * 加载会话页面 ConversationFragment
	 * 
	 * @param mConversationType
	 *            会话类型
	 * @param mTargetId
	 *            目标 Id
	 */
	@SuppressLint("DefaultLocale")
	private void enterFragment(Conversation.ConversationType mConversationType,
			String mTargetId) {

		ConversationFragment fragment = (ConversationFragment) getSupportFragmentManager()
				.findFragmentById(R.id.conversation);

		Uri uri = Uri.parse("rong://" + getApplicationInfo().packageName)
				.buildUpon().appendPath("conversation")
				.appendPath(mConversationType.getName().toLowerCase())
				.appendQueryParameter("targetId", mTargetId).build();

		fragment.setUri(uri);
	}

	@Override
	public void onClick(View arg0) {
		int viewId = arg0.getId();
		switch(viewId)
		{
		case R.id.right:
			if(mConversationType == Conversation.ConversationType.GROUP) 
			{
				Intent intent = new Intent(this,ClassmateActivity.class);
				intent.putExtra("name", getIntent().getData().getQueryParameter("title").toString());
				intent.putExtra("userId",mTargetId);
				startActivity(intent);
			}
			else
			{
				Intent intent = new Intent(this,PersonInfoActivity.class);
				intent.putExtra("name", getIntent().getData().getQueryParameter("title").toString());
				intent.putExtra("userId",mTargetId);
				intent.putExtra("flag", 1);
				startActivity(intent);
			}
			break;
			
		case R.id.left:
			finish();
			break;
		}
	}
	
	@Override  
	public Resources getResources() {  
	    Resources res = super.getResources();    
	    Configuration config=new Configuration();    
	    config.setToDefaults();    
	    res.updateConfiguration(config,res.getDisplayMetrics() );  
	    return res;  
	}  
}
